<template>
  <label
    :class="[
      'hm-radio',
      {
        'is-checked': label === model
      }
    ]"
  >
    <span class="hm-radio__input">
      <span class="hm-radio__inner"></span>
      <input class="hm-radio__original" type="radio" :value="label" :name="name" v-model="model" />
    </span>
    <span class="hm-radio__label">
      <slot></slot>
      <!-- 如果没有默认插槽 就把 label 属性作为默认展示 -->
      <template v-if="!$slots.default">{{ label }}</template>
    </span>
  </label>
</template>

<script>
export default {
  name: 'HmRadio',
  inject: {
    RadioGroup: {
      default: ''
    }
  },
  props: {
    label: {
      type: [String, Number, Boolean],
      default: ''
    },
    value: null,
    name: {
      type: String,
      default: ''
    }
  },
  computed: {
    model: {
      get () {
        return this.isGroup ? this.RadioGroup.value : this.value
      },
      set (newVal) {
        // 派发给父组件 input 事件
        // this.$emit('input', newVal)
        this.isGroup ? this.RadioGroup.$emit('input', newVal) : this.$emit('input', newVal)
      }
    },
    isGroup () {
      // 用于判断 radio 是否被 RadioGroup 所包裹
      return !!this.RadioGroup // “!!”将任何类型的值转换为相应的 Boolean 值
    }
  }
}
</script>

<style lang="scss">
  .hm-radio {
    color: #606266;
    font-weight: 500;
    line-height: 1;
    position: relative;
    cursor: pointer;
    display: inline-block;
    white-space: nowrap;
    outline: none;
    font-size: 14px;
    margin-right: 30px;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;

    .hm-radio__input {
      white-space: nowrap;
      cursor: pointer;
      outline: none;
      display: inline-block;
      line-height: 1;
      position: relative;
      vertical-align: middle;

      .hm-radio__inner {
        border: 1px solid #dcdfe6;
        border-radius: 100%;
        width: 14px;
        height: 14px;
        background-color: #fff;
        position: relative;
        cursor: pointer;
        display: inline-block;
        box-sizing: border-box;

        &::after {
          width: 4px;
          height: 4px;
          border-radius: 100%;
          background-color: #fff;
          content: '';
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%) scale(0);
          transition: transform 0.15s ease-in;
        }
      }

      .hm-radio__original {
        opacity: 0;
        outline: none;
        position: absolute;
        z-index: -1;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: 0;
      }
    }

    .hm-radio__label {
      font-size: 14px;
      padding-left: 10px;
    }
  }
  // 选中时样式
  .hm-radio.is-checked {
    .hm-radio__input {
      .hm-radio__inner {
        border-color: #409eff;
        background: #409eff;
        &::after {
          transform: translate(-50%, -50%) scale(1);
        }
      }
    }
    .hm-radio__label {
      color: #409eff;
    }
  }
</style>
